var mongodb = require('./mongodb');

var Schema = mongodb.mongoose.Schema;

var ImageSchema = new Schema({
    pub_time: String,
    descriptione: String,
    description_ene: String,
    heighte: String,
    photo_user: [{
        user_ide: String,
        user_photo: String,
        user_name: String,
        user_link: String
    }],
    width: String,
    up_times: String,
    desc_user: [{
        user_id: String,
        user_photo: String,
        user_name: String
    }],
    image_url: String
});

var ImageModel = mongodb.mongoose.model("imagesList", ImageSchema);


function saveOneImageToLibrary(image, callback) {
    var imageModel = new ImageModel(image);
    imageModel.save(function (err) {
        if (!err) {
            console.log('保存成功');
        } else {
            console.log(err + '保存失败');
        }
        callback();
    });
}

function saveImageArrayToLibrary(imageArray, callback) {
    ImageModel.update(imageArray, function (err, jellybean, snickers) {
        if (err) {
            console.log('批量保存失败');
        } else {
            console.log('批量保存成功');
            callback();
        }
    });
}

function findAllImage(callback) {
    ImageModel.find({}, function (err, results) {
        callback(results);
    });
}


exports.saveOneImageToLibrary = saveOneImageToLibrary;
exports.saveImageArrayToLibrary = saveImageArrayToLibrary;
exports.findAllImage = findAllImage;
